//-----------------------------------------------------------------------
// <copyright file="a_CatGroupDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_CatGroupDAOBase
    {
        #region Common methods
        public virtual a_CatGroup Createa_CatGroupFromReader(IDataReader reader)
        {
            a_CatGroup item = new a_CatGroup();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("GroupID"))) item.GroupID = (int)reader["GroupID"];
                if (!reader.IsDBNull(reader.GetOrdinal("GroupName"))) item.GroupName = (string)reader["GroupName"];
                if (!reader.IsDBNull(reader.GetOrdinal("UGroupName"))) item.UGroupName = (string)reader["UGroupName"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_CatGroup methods
            
        public virtual void Createa_CatGroup(a_CatGroup a_CatGroup)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatGroup_ins");
                
                database.AddInParameter(dbCommand, "@GroupName", DbType.AnsiString, a_CatGroup.GroupName);
                database.AddInParameter(dbCommand, "@UGroupName", DbType.String, a_CatGroup.UGroupName);
                database.AddOutParameter(dbCommand, "@GroupID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_CatGroup.GroupID = (int)database.GetParameterValue(dbCommand, "@GroupID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_CatGroup methods
        
        public virtual void Updatea_CatGroup(a_CatGroup a_CatGroup)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatGroup_upd");            
                
                database.AddInParameter(dbCommand, "@GroupID", DbType.Int32, a_CatGroup.GroupID);
                database.AddInParameter(dbCommand, "@GroupName", DbType.AnsiString, a_CatGroup.GroupName);
                database.AddInParameter(dbCommand, "@UGroupName", DbType.String, a_CatGroup.UGroupName);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_CatGroup methods
        public virtual void Deletea_CatGroup(int groupID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatGroup_del");
                
                database.AddInParameter(dbCommand, "@GroupID", DbType.Int32, groupID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_CatGroup methods
        
        public virtual a_CatGroup Geta_CatGroup(int groupID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatGroup_sel");
                
                database.AddInParameter(dbCommand, "@GroupID", DbType.Int32, groupID);
                
                a_CatGroup a_CatGroup =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_CatGroup = Createa_CatGroupFromReader(reader);
                        reader.Close();
                    }
                }
                return a_CatGroup;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_CatGroupList methods
        public virtual a_CatGroupCollection Geta_CatGroupList(a_CatGroupColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatGroup_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_CatGroupCollection a_CatGroupCollection = new a_CatGroupCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_CatGroup a_CatGroup = Createa_CatGroupFromReader(reader);
                        a_CatGroupCollection.Add(a_CatGroup);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CatGroupCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_CatGroupCollection Geta_CatGroupList(a_CatGroupColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_CatGroupList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

        #region Geta_CatGroupListBy Alphabet methods
        public virtual a_CatGroupCollection Geta_CatGroupList_alphabet(string mychars, a_CatGroupColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatGroup_selList_alphabet");

                database.AddInParameter(dbCommand, "@Mychar", DbType.AnsiString, mychars);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_CatGroupCollection a_CatGroupCollection = new a_CatGroupCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_CatGroup a_CatGroup = Createa_CatGroupFromReader(reader);
                        a_CatGroupCollection.Add(a_CatGroup);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CatGroupCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_CatGroupCollection Geta_CatGroupList_alphabet(string mychar, a_CatGroupColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_CatGroupList_alphabet(mychar, orderBy, orderDirection, 0, 0, out totalRecords);
        }
        #endregion
    }
}